package com.liyi.service;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.liyi.dao.DepDao;
import com.liyi.dao.EmpDao;
import com.liyi.util.ServiceUtil;
import com.zero.basic.exception.SmsErrorConstant;
import com.zero.basic.exception.SmsRuntimeException;
import com.zero.basic.exception.SmsStatusCode;
import com.zero.mms.model.Employee;

@Service("empServiceLiyi")
@Transactional(rollbackFor = Exception.class)
public class EmpService {

	@Inject
	private EmpDao empDaoLiyi;
	@Inject
	private DepDao depDaoLiyi;
	
	/* 获取某 [部门] 所有 [员工] 的id */
	public List<Integer> getEmpIdsByDepId(Integer depId) throws SmsRuntimeException {
		try {
			List<Integer> childDepsIds = depDaoLiyi.getChildDepsByParentId(depId);
			String allDepIds = ServiceUtil.listIntegerToCommaString(childDepsIds);
			List<Integer> empIds = empDaoLiyi.getEmpIdsByDepIds(allDepIds);
			return empIds;
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR, SmsErrorConstant.WEB_APPLICATION, "服务器异常");
		}
	}
	
	/* 根据 [员工]id 获得 [员工]姓名 */
	public String getEmpNameByEmpId(Integer empId) throws SmsRuntimeException {
		try {
			String empName = empDaoLiyi.getEmpNameByEmpId(empId);
			return empName;
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR, SmsErrorConstant.WEB_APPLICATION, "服务器异常");
		}
	}
	
	/* 根据 [部门]id 获取 [员工]列表 */
	public List<Employee> getEmpsByDepId(Integer depId) throws SmsRuntimeException {
		try {
			List<Integer> childDepsIds = depDaoLiyi.getChildDepsByParentId(depId);
			String allDeps = ServiceUtil.listIntegerToCommaString(childDepsIds);
			List<Employee> emps = empDaoLiyi.getEmpsByDepId(allDeps);
			return emps;
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR, SmsErrorConstant.WEB_APPLICATION, "服务器异常");
		}
	}
}
